package com.killer.mianshibaodian;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.killer.mianshibaodian.utils.Globals;
import com.killer.mianshibaodian.utils.QuestionDao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_index);


        new Thread(){
            @Override
            public void run() {



                try {
                    InputStream is = getAssets().open("question.txt");
                    SharedPreferences preferences = getSharedPreferences("question_flag", MODE_PRIVATE);
                    boolean saveFlag = preferences.getBoolean("flag",false);
                    if (saveFlag){
                        sleep(2000);
                    }else {
                        QuestionDao.deleteAllData();
                        loadInputStream(is);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putBoolean("flag",true);
                        editor.commit();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent(IndexActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        }.start();

    }

    private List<Map<String, Object>> loadInputStream(InputStream is) throws IOException {
        List<Map<String, Object>> values = new ArrayList<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        //存放问题
        StringBuffer question = new StringBuffer();
        //存放答案
        StringBuffer answer = new StringBuffer();
        boolean questionFlag = false;
        while ((line=br.readLine())!=null){
            if (line.equals("Start_Question_Flag")){
                questionFlag = true;
            }else if (line.equals("Start_Answer_Flag")){
                questionFlag = false;
            }else if (line.equals("End")){
                //读取结束
                Map<String,Object> map = new HashMap<>();
                map.put("question",question.toString());
                map.put("answer",answer.toString());

                QuestionDao.insertData(map);
                //清空，方便下次使用
                question = new StringBuffer();
                answer = new StringBuffer();

            }else {
                //读取数据
                if (questionFlag){
                    question.append(line);
                }else {
                    answer.append(line);

                }
            }
        }

        return null;
    }
}
